package com.rockcent.mall.lib.repository;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.rockcent.common.common.SQLListDoing;
import com.rockcent.common.common.SQLObjectDoing;
import com.rockcent.mall.lib.domain.AuthResource;
import com.rockcent.mall.lib.dto.AuthResourceDto;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by DK on 17/3/10.
 */
@Repository
public class AuthResourceRepositoryImpl implements AuthResourceRepositoryCustom {

    private static final Logger log = LoggerFactory.getLogger(AuthResourceRepositoryImpl.class);

    @Autowired
    NamedParameterJdbcTemplate jdbcTemplate;

    @Autowired
    private ObjectMapper objectMapper;

    @Override
    public List<AuthResourceDto> findAuthResourceDtoList(String systemType) {
        SQLListDoing<AuthResourceDto> doing = (sql, fields, params, typeReferenceMap) -> {
            sql.append("select %s from auth_resource ar where 1=1 ");
            sql.append(" and ar.system_type = :systemType");
            sql.append(" and ar.is_delete = 'NO'");
            params.put("systemType", systemType);
            sql.append(" order by ar.id asc");
            fields.append("ar.*");
        };
        return doing.go(AuthResourceDto.class, jdbcTemplate, log, objectMapper);
    }

    @Override
    public List<AuthResource> findAuthResourceList(String systemType, Long id) {
        SQLListDoing<AuthResource> doing = (sql, fields, params, typeReferenceMap) -> {
            sql.append("select %s from auth_resource ar where 1=1 ");
            if (id != null) {
                sql.append(" and ar.parent_id = :id");
                params.put("id", id);
            }
            sql.append(" and ar.system_type = :systemType");
            sql.append(" and ar.is_delete = 'NO'");
            params.put("systemType", systemType);
            sql.append(" order by ar.id asc");
            fields.append("ar.*");
        };
        return doing.go(AuthResource.class, jdbcTemplate, log, objectMapper);
    }

    @Override
    public Long findMaxSortByParentId(Long parentId, String systemType) {
        SQLObjectDoing<Long> doing = (sql, fields, params, typeReferenceMap) -> {
            sql.append("select %s from auth_resource ar where 1=1 ");
            if (parentId != null) {
                sql.append(" and ar.parent_id = :parentId");
                params.put("parentId", parentId);
            }
            if (StringUtils.isNotBlank(systemType)) {
                sql.append(" and ar.system_type = :systemType");
                params.put("systemType", systemType);
            }
            fields.append("max(ar.sort)");
        };
        return doing.go(Long.class, jdbcTemplate, log, objectMapper);
    }

    @Override
    public List<AuthResourceDto> findAuthResourceDtoList(List<Long> resourceIdList, String systemType) {
        SQLListDoing<AuthResourceDto> doing = (sql, fields, params, typeReferenceMap) -> {
            sql.append("select %s from auth_resource ar where 1=1 ");
            sql.append(" and ar.id in (:resourceIdList)");
            sql.append(" and ar.is_delete = 'NO'");
            params.put("resourceIdList", resourceIdList);
            params.put("systemType", systemType);
            sql.append(" order by ar.id asc");
            fields.append("ar.*");
        };
        return doing.go(AuthResourceDto.class, jdbcTemplate, log, objectMapper);
    }
}
